<template>
	<view class="add-box">
		<view class="item" v-for="(item,index) in list" :key="index" :item-id="index">
			<view class="content-box">
				<view class="t1">
					{{item.name}} <text>{{item.phone}}</text>
				</view>
				<view class="t2">
					<text class="mr" v-if="item.isDefault">默认</text>{{item.areaName}}{{item.address}}
				</view>
				<view v-if="choose" class="chooseArea" @click="chooseAddr(item.id)"></view>
			</view>
			<view class="btn-box">
				<!-- <view v-if="choose" class="btn" @click="chooseAddr(item.id)">选择</view> -->
				<view class="btn" :data-id="item" @click="edit">编辑</view>
				<view class="btn" :data-id="item.id" @click="del">删除</view>
			</view>
		</view>


		<navigator url="/pages/site/addsite" class="red-btn">新增收货地址</navigator>
	</view>
</template>

<script>
	import {
		getReceiver
	} from '@/api/user.js';
	import {
		delReceiver
	} from '@/api/common.js';
	export default {
		data() {
			return {
				list: [],
				choose: false
			};
		},
		onLoad(options) {
			if(options.choose) {
				this.choose = options.choose
			}
			// this.init();
		},
		onShow() {
			this.init()
		},
		methods: {
			init() {
				getReceiver().then(res => {
					console.log(res)
					this.list = res.data.receiverList
				})
			},
			chooseAddr(id) {
				uni.setStorageSync('addrId', id)
				uni.navigateBack()
			},
			edit(e) {
				console.log(e)
				var item = e.currentTarget.dataset.id

				uni.navigateTo({
					url: '/pages/site/addsite?status=1&item=' + encodeURIComponent(JSON.stringify(item))
				})

			},
			del(e) {
				var id = e.currentTarget.dataset.id
				delReceiver({
					id: id
				}).then(res => {
					console.log(res)
					this.init();
				})
			}

		}
	}
</script>

<style lang="scss">
	page {
		background-color: #f5f5f5;
	}

	.add-box {

		.item {
			width: 100%;
			padding: 30rpx;
			background-color: #fff;
			margin-top: 30rpx;

			.t1 {
				color: #1c1c1c;
				font-size: 24rpx;
				font-weight: 600;
				margin-bottom: 30rpx;

				text {
					color: #898989;
					font-size: 24rpx;
					margin-left: 20rpx;
					font-weight: normal;
				}
			}

			.t2 {
				font-size: 24rpx;
				color: #898989;
				line-height: 1.5;
				padding-bottom: 40rpx;
				border-bottom: 1px solid #f0f0f0;
				margin-bottom: 30rpx;

				.mr {
					margin-right: 15rpx;
					font-size: 16rpx;
					color: #fff;
					padding: 4rpx 8rpx;
					background-color: #b81c22;
				}
			}

			.btn-box {
				text-align: right;
				display: flex;
				align-items: center;
				justify-content: flex-end;

				.btn {
					width: 90rpx;
					height: 44rpx;
					line-height: 40rpx;
					text-align: center;
					background-color: #ffffff;
					border: solid 1px #000000;
					margin-left: 15rpx;
					font-size: 28rpx;
					color: #1c1c1c;
					letter-spacing: 3rpx;
				}
			}

		}

	}

	.red-btn {
		position: fixed;
		left: 50%;
		bottom: 30rpx;
		transform: translateX(-50%);
		width: 92%;
		text-align: center;
		height: 90rpx;
		line-height: 90rpx;
		background: #b81c22;
		color: #ffffff;
		font-size: 24rpx;
	}
	
	.content-box {
		position: relative;
		
		.chooseArea {
			position: absolute;
			top: 0;
			left: 0;
			width: 100%;
			height: 100%;
		}
	}
</style>
